package com.monitor.zabbix.service;

import java.util.Map;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ZabbixApiUtil {

	private static String testApiUrl = "http://123.56.67.133/zabbix/api_jsonrpc.php";
	private static String username = "Admin";
	private static String password = "zabbix";
	private static String auth = "287706085fb117449023a558e7392493";

	protected static Logger logger = LoggerFactory
			.getLogger(ZabbixApiUtil.class);

	public static Map<String, Object> request(String apiUrl, String method,
			Object params, String auth) throws Exception {
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("jsonrpc", "2.0");
		jsonObj.put("method", method);
		jsonObj.put("params", params);
		jsonObj.put("id", 1);
		if (auth != null && !"".equals(auth)) {
			jsonObj.put("auth", auth);
		}
		System.out.println("请求参数:" + jsonObj.toString() + ";请求url:" + apiUrl);
		String result = HttpClientUtil.postReq(apiUrl, jsonObj.toString());
		Map<String, Object> responseMap = JSON.parseObject(result);
		System.out.println("result=" + result);
		return responseMap;
	}

	@Test
	public void login() throws Exception {
		JSONObject params = new JSONObject();
		params.put("user", username);
		params.put("password", password);
		request(testApiUrl, "user.login", params, "");
	}

	@Test
	public void getHost() throws Exception {
		JSONObject params = new JSONObject();
		params.put("output", "extend");
		// params.put("password", password);
		request(testApiUrl, "host.get", params, auth);
	}

	@Test
	public void getHostGroup() throws Exception {
		JSONObject params = new JSONObject();
		params.put("output", "extend");
		// params.put("password", password);
		request(testApiUrl, "hostgroup.get", params, auth);
	}

	@Test
	public void createHost() throws Exception {
		JSONObject params = new JSONObject();
		params.put("host", "test_guoxc");
		JSONObject interfaces = new JSONObject();
		interfaces.put("type", 1);
		interfaces.put("main", 1);
		interfaces.put("useip", "1");
		interfaces.put("ip", "192.168.3.1");
		interfaces.put("dns", "");
		interfaces.put("port", "10050");

		params.put("interfaces", new JSONObject[] { interfaces });

		JSONObject groups = new JSONObject();
		groups.put("groupid", 1);
		params.put("groups", new JSONObject[] { groups });
		request(testApiUrl, "host.create", params, auth);
	}

	@Test
	public void itemGet() throws Exception {
		JSONObject params = new JSONObject();
		params.put("output", "extend");
		params.put("limit", 10);
		request(testApiUrl, "item.get", params, auth);
	}

	@Test
	public void historyGet() throws Exception {
		JSONObject params = new JSONObject();
		params.put("history", 0);
       
		params.put("sortfield",  "clock");
		params.put( "sortorder", "DESC");
//		params.put("time_from", System.currentTimeMillis() - 60 * 60 * 24 * 7
//				* 1000);
//		params.put("time_till", System.currentTimeMillis());

		params.put("itemids",  new String[]{"23296"});
		params.put("output", "extend");
		request(testApiUrl, "history.get", params, auth);
	}

	@Test
	public void alertGet() throws Exception {
		JSONObject params = new JSONObject();
		params.put("history", 0);
		params.put("itemids", new String[] { "10009", "10014", "10016" });
		params.put("time_from", System.currentTimeMillis() - 60 * 60 * 24 * 30
				* 1000);
		params.put("time_till", System.currentTimeMillis());
		params.put("output", "extend");
		request(testApiUrl, "alert.get", params, auth);
	}

	@Test
	public void templateGet() throws Exception {
		JSONObject params = new JSONObject();
		// params.put("itemids", new String[]{"10014","10016"});

		params.put("output", "extend");
		request(testApiUrl, "template.get", params, auth);
	}
	
	@Test
	public void hostIsAvailable() throws Exception {
		 
		request(testApiUrl, "host.isreadable", new String[]{"10107"}, auth);
	}

	@Test
	public void hostIswritable() throws Exception {
		 
		request(testApiUrl, "host.iswritable", new String[]{"10107"}, auth);
	}

}
